package com.komorebishao.crm.controller;

import com.komorebishao.crm.dto.LoginResult;
import com.komorebishao.crm.pojo.CrmUser;
import com.komorebishao.crm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @Author: KomorebiShao
 * @Description:
 * @Date: Created in 2020-07-08
 * @Modified By:
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
    //显示login.jsp
    @RequestMapping("/login")
    public String showLogin(){
        return "login";
    }

    @RequestMapping("/doLogin")
    public String userLogin(HttpSession session, String usercode, String password, Model model){
        //调用service判断
        LoginResult result = userService.login(usercode,password);
        //如果登陆成功，需要保存身份信息到session中
        if (result.getUser() != null){
            session.setAttribute("user",result.getUser());
            //请求转发，去到列表页面
            return "forward:/customerList";
        } else {
            //将错误信息放入model
            model.addAttribute("msg",result.getMsg());
            //回到登陆
            return "login";
        }
    }
}
